package com.czr.service.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.entity.Collect;
import com.czr.mapper.CollectMapper;
import com.czr.service.collectcategory.CollectCategoryService;
import com.czr.vo.CollectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CollectDictServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private CollectCategoryService collectCategoryService;

    @Override
    public List<CollectVo> findList() {
        log.info("--------------> collectDictService");
        // 1： 查询表中所有的收藏夹的数据
        LambdaQueryWrapper<Collect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Collect::getStatus, 1);
        // 2: 查询所有数据
        List<Collect> collectList = this.list(lambdaQueryWrapper);
        // 3: 根据分类进行分组
        Map<Integer, List<Collect>> collectMap = collectList.stream().collect(Collectors
                .groupingBy(Collect::getCategoryId));
        // 4: 根据分组的分类ID去收藏数据中进行查询
        List<CollectVo> collectVoList = new ArrayList<>();
        for (Map.Entry<Integer, List<Collect>> entry : collectMap.entrySet()) {
            CollectVo collectVo = new CollectVo();
            collectVo.setCategoryId(entry.getKey());
            collectVo.setCategoryTitle(getCategory(entry.getKey()));
            collectVo.setCollectList(entry.getValue());
            collectVoList.add(collectVo);
        }
        // 6: 返回即可
        return collectVoList;
    }


    public String getCategory(Integer cid) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Java面试");
        map.put(2, "Web开发");
        map.put(3, "设计系统");
        map.put(4, "框架服务");
        map.put(5, "微服务");
        map.put(6, "移动APP");
        return map.get(cid);
    }

}